# 用方向键控制方块移动
# 方向键松开, 停止移动
# 碰撞后减1分, 重新碰撞再减一分
# 小rect追逐大rect 已实现
# 每5秒增加一个敌人rect 已实现
# 在循环中碰撞判断 已实现
# 白色rect 用来增加分数
# 优化分数为零后的分数显示
# 优化方向键的控制
# Nasa 2021121
import random

import pygame
import sys
from pygame_ok.my_class import TheRect
from pygame_ok.pygame09 import zhui


def move():
    pygame.init()
    winx = 400
    winy = 400
    screen = pygame.display.set_mode((winx, winy))
    fclock = pygame.time.Clock()
    r1x = 51
    r1y = 60
    speedx = 5
    speedy = 5
    b = 'still'
    f = 10  # 分数
    # c = 'no'  # 碰撞标记
    added = 0  # 增加方块标记 0:尚未增加 1:已增加
    added_white = 1  # 增加白点标记 0:不增加 1:要增加
    r2x = 10
    r2y = 20
    r2speedx = 1
    r2speedy = 1
    r2 = TheRect(r2x, r2y, 20, 20, (255, 255, 0), 0)
    list = []
    r1 = TheRect(r1x, r1y, 50, 50, (255, 255, 255))
    list_white = []
    font_time = 1  # 停止走时间标记 1:走  0:不走

    while True:
        if f>0:
            t = pygame.time.get_ticks() // 1000
        if t % 5 == 0:
            if added == 0:
                list.append(TheRect(random.randint(0, winx),
                                    random.randint(0, winy),
                                    20, 20, (255, 255, 0), 0))
                print('增加方块', t)
                added = 1

        else:
            added = 0
        screen.fill((0, 0, 0))
        for r in list:
            r.draw(screen)
            r.move(r2speedx * zhui(r.x, r.y, r1.x, r1.y)[0],
                   r2speedy * zhui(r.x, r.y, r1.x, r1.y)[1])
            if r1.is_collided(r) and f > 0:
                if r.c == 'no':
                    f -= 1
                    r.c = 'yes'
                    print('collide', f, r.c)
            else:
                r.c = 'no'
        r1.draw(screen)

        if added_white == 1:
            list_white.append(TheRect(random.randint(0, winx - 20),
                                      random.randint(0, winy - 20),
                                      20, 20, (255, 255, 255)))
            added_white = 0
        for white in list_white:
            white.draw(screen)
            if white.is_collided(r1):
                f += 2
                list_white.remove(white)
                added_white = 1

        font = pygame.font.Font(None, 30)
        if f > 0:
            if font_time == 1:
                text = font.render(f'score:{f} time:{t}', True, (255, 255, 255))
                screen.blit(text, (2, 2))
        else:
            font_time = 0

            # dt.append(t)
            text2 = font.render(f'time:{t}', True, (255, 255, 255))
            screen.blit(text2, (2, 2))

        if f > 0:
            if b == 'down':
                r1.move(0, speedy)
            elif b == 'up':
                r1.move(0, speedy * -1)
            elif b == 'left':
                r1.move(speedx * -1, 0)
            elif b == 'right':
                r1.move(speedx, 0)
        else:
            print('游戏结束!')

        if r1.x + 20 < 0:
            r1.x = winx
        elif r1.x > winx:
            r1.x = -20
        if r1.y + 20 < 0:
            r1.y = winy
        elif r1.y > winy:
            r1.y = -20

        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if e.type == pygame.KEYDOWN:
                if e.key == pygame.K_DOWN:
                    b = 'down'
                elif e.key == pygame.K_UP:
                    b = 'up'
                elif e.key == pygame.K_LEFT:
                    b = 'left'
                elif e.key == pygame.K_RIGHT:
                    b = 'right'
            elif e.type == pygame.KEYUP:
                if e.key == pygame.K_DOWN and b == 'down':
                    b = 'still'
                elif e.key == pygame.K_UP and b == 'up':
                    b = 'still'
                elif e.key == pygame.K_LEFT and b == 'left':
                    b = 'still'
                elif e.key == pygame.K_RIGHT and b == 'right':
                    b = 'still'
        pygame.display.update()
        fclock.tick(10)


if __name__ == '__main__':
    move()
